package com.ggh.jinjilive.ui.mine.address;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityTheGoodsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mine.address.adapter.AddressAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Route(path = ARouterUtils.TheGoodsActivity)
public class TheGoodsActivity extends BaseActivity<ActivityTheGoodsBinding> implements AddItemOnClickListener{
    //VM
    private TheGoodsViewModel affirmOrderViewModel;
    //收货地址适配器
    private AddressAdapter addressAdapter;
    //获取传输的地址
    private TheGoodsAddressMobile mTheGoodsAddressMobile;
    private String distinguish;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        affirmOrderViewModel = new ViewModelProvider(this,viewModelFactory).get(TheGoodsViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_the_goods;

    }

    @Override
    public void initParam() {
        super.initParam();
        mTheGoodsAddressMobile = (TheGoodsAddressMobile) getIntent().getSerializableExtra("mTheGoodsAddressMobile");
        distinguish = getIntent().getStringExtra("distinguish");
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("收货地址",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        //收货地址适配器
        if(null != mTheGoodsAddressMobile){
            addressAdapter = new AddressAdapter(mTheGoodsAddressMobile,distinguish);
        }else {
            addressAdapter = new AddressAdapter(distinguish);
        }

        addressAdapter.setAddItemOnClickListener(this);
        binding.rvAddress.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvAddress.setAdapter(addressAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        showDialog();
        //获取收货地址
        affirmOrderViewModel.receiveAddressList();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //获取收货地址
        affirmOrderViewModel.TheGoodsAddressMobileList.observe(this, new Observer<List<TheGoodsAddressMobile>>() {
            @Override
            public void onChanged(List<TheGoodsAddressMobile> theGoodsAddressMobiles) {
                if(null != theGoodsAddressMobiles){
                    theGoodsAddressMobiles.add(null);
                    addressAdapter.setAdd(theGoodsAddressMobiles);
                }
                cancelDialog();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //获取收货地址
        affirmOrderViewModel.receiveAddressList();
    }

    @Override
    public void onSkipClicked(int position, Object mObject) {
        if(position == 9999){//添加收货地址
            Intent intent =new Intent(mContext, AddTheGoodsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);//启动Activity
        }else if(position == 9998){//取消默认或者设置默认
            TheGoodsAddressMobile theGoodsAddressMobile = (TheGoodsAddressMobile) mObject;
            Map<String, Object> map = new HashMap<>();
            map.put("is_default",theGoodsAddressMobile.getIs_default());
            map.put("name",theGoodsAddressMobile.getName());
            map.put("mobile",theGoodsAddressMobile.getMobile());
            map.put("code","012");
            map.put("content",theGoodsAddressMobile.getContent());
            map.put("s_content",theGoodsAddressMobile.getS_content());
            map.put("id",theGoodsAddressMobile.getId());
            //修改地址
            affirmOrderViewModel.editReceiveAddress(map);
        }else if(position == 9997){//使用当前地址
            TheGoodsAddressMobile theGoodsAddressMobile = (TheGoodsAddressMobile) mObject;
            Intent intent = getIntent();
            intent.putExtra("theGoodsAddressMobile", theGoodsAddressMobile);
            setResult(105,intent);
            finish();
        }else if(position == 9996){
            TheGoodsAddressMobile theGoodsAddressMobile = (TheGoodsAddressMobile) mObject;
            //删除地址
            affirmOrderViewModel.delReceiveAddress(theGoodsAddressMobile.getId());
        }else if(position == 9995){
            TheGoodsAddressMobile theGoodsAddressMobile = (TheGoodsAddressMobile) mObject;
            if(null != theGoodsAddressMobile){
                //修改地址
                ARouter.getInstance().build(ARouterUtils.AddTheGoodsActivity)
                        .withSerializable("theGoodsAddressMobile",theGoodsAddressMobile)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(TheGoodsActivity.this,1095);
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 1095:
                //获取收货地址
                affirmOrderViewModel.receiveAddressList();
                break;
        }
    }
}
